﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QCOrder;
using System.Configuration;
using CommonClasses;

namespace QCOrder
{
    public class Order : IOrders
    {
        private int orderID;

        public int OrderID
        {
            get { return orderID; }
            set { orderID = value; }
        }

        private int materialID;

        public int MaterialID
        {
            get { return materialID; }
            set { materialID = value; }
        }

        private int machineID;

        public int MachineID
        {
            get { return machineID; }
            set { machineID = value; }
        }

        private DateTime startDate;

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        private DateTime endDate;

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public int EmpID { get { return EmpID; } set { this.EmpID = value; } }
        
      private string connectionString { get; set ;}

      public Order()
      {
          connectionString = ConfigurationManager.ConnectionStrings["Order"].ToString();

      }
    

      public DataTable GetCurrentDayOrders(DateTime StartDate, DateTime EndDate)
      {

          DataTable dtCurrentOrders = new DataTable();
          foreach (DataRow dr in dtCurrentOrders.Rows)
          {
              dtCurrentOrders.Columns.Add("MachineID", typeof(int));
              dtCurrentOrders.Columns.Add("MaterialID", typeof(int));
              dtCurrentOrders.Columns.Add("StartDate", typeof(DateTime));
              dtCurrentOrders.Columns.Add("EndDate", typeof(DateTime));
          }

          try
          {
              string sql = "Select * FROM [Order] where StartDate = '" + StartDate + "'OR EndDate ='" + EndDate + "'";

              using (SqlConnection myConnection = new SqlConnection(connectionString))
              {
                  using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
                  {
                      myConnection.Open();
                      using (SqlDataReader myReader = myCommand.ExecuteReader())
                      {
                          dtCurrentOrders.Load(myReader);
                         
                      }
                  }
              }
          }
          catch (SqlException ex)
          {
              string error = string.Empty;
              Log log = new Log();
              log.LogTransaction(EmpID, " ", ex.Message, error);
              throw ex;

          }
          return dtCurrentOrders;
      }
    


      public DataTable GetFutureOrders(DateTime StartDate, DateTime EndDate)
      {
          
          DataSet dataset = new DataSet();

          DataTable dtFutureOrders = new DataTable();

          try
          {

              string sql = "Select * FROM [Order] where [StartDate] >= '" + StartDate + "'AND [EndDate] <='" + EndDate + "'";


              using (SqlConnection myConnection = new SqlConnection(connectionString))
              {
                  using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
                  {
                      myConnection.Open();
                      using (SqlDataReader myReader = myCommand.ExecuteReader())
                      {

                          dtFutureOrders.Load(myReader);
                          
                      }
                  }
              }
          }
          catch(SqlException ex)
          {
              string error = string.Empty;
              Log log = new Log();
              log.LogTransaction(EmpID, " ", ex.Message, error);
              throw ex;
          }
          
          return dtFutureOrders;
         
      }

    
    }

   
}
